# 导入mysql驱动
import mysql.connector


'''
dev 环境
username: limsadmin
password: dj36#oc47
url: jdbc:mysql://3306-W-G-LTM-DEV-01-MYC1.service.dc_sd.consul:3306/midea_lims_new?serverTimezone=Hongkong
hostName: http://limsdev.midea.com
'''


# 连接mysql数据库
def connect_mysql():
    # 数据库连接配置
    config = {
        'host': '3306-W-G-LTM-DEV-01-MYC1.service.dc_sd.consul',  # 主机地址
        'user': 'limsadmin',  # 用户名
        'password': 'dj36#oc47',  # 密码
        'database': 'midea_lims_new' # 数据库名
    }

    try:
        # 创建连接
        cnx = mysql.connector.connect(**config)
        print("连接成功")
        return cnx
    except mysql.connector.Error as err:
        print("连接失败：{}".format(err))

# 关闭连接
def close_mysql(cnx):
    cnx.close()

# 查询表scadaequipchecknodeinfo
def select_scadaequipchecknodeinfo(cnx, equipmentNo):
    # 创建游标
    cursor = cnx.cursor()
    # 查询语句
    sql = "select acode,checkNodeId,checkNodeType from scadaequipchecknodeinfo where equipmentNo = '"+equipmentNo+"'"
    # 执行查询
    cursor.execute(sql)
    # 获取查询结果
    result = cursor.fetchall()
    # 关闭游标
    cursor.close()
    return result

def select_scadaequipnodeinfo(equipmentNo):
    cnx = connect_mysql()
    result = select_scadaequipchecknodeinfo(cnx, equipmentNo)
    if cnx.is_connected():
        cnx.close()
    return result


# # 测试连接
# cnx = connect_mysql()
#
# result = select_scadaequipchecknodeinfo(cnx)
# # print(result)
# # 循环输出result中的记录
# for row in result:
#     print(row)
# if cnx:
#     close_mysql(cnx)


# 测试是否能连接到mysql
def test_connect_mysql():
    cnx = connect_mysql()
    if cnx:
        close_mysql(cnx)